using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Seer.SeerSocket;

namespace 常驻后台的数据导出服务.Controllers;

[ApiController]
[Route("[controller]/[action]")]
public class SeerController : ControllerBase
{
	private readonly IDistributedCache distCache;
	private readonly IDistributedCacheHelper helper;
	private readonly SeerSocket seerSocket;
	public SeerController(IDistributedCache distCache, IDistributedCacheHelper helper,SeerSocket seerSocket)
	{
		this.distCache = distCache;
		this.helper = helper;
		this.seerSocket = seerSocket;
	}

	[HttpGet]
	public string Now()
	{
		string s = distCache.GetString("Now");
		if (s == null)
		{
			s = DateTime.Now.ToString();
			var opt = new DistributedCacheEntryOptions();
			opt.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(180);
			distCache.SetString("Now", s, opt);
		}
		return s;
	}

	[HttpGet]
	public Task<string?> Now2()
	{
		return helper.GetOrCreateAsync<string>("all1", async e => this.seerSocket.Send(0x044c));
	}

}



